﻿using System;
using System.Security.Permissions;


namespace DeveloperTeam
{
    public class NumberToLetter
    {
        /// <summary>
        /// Convert Number to Persian Letters
        /// </summary>
        /// <param name="number">Number for convert to Letter</param>
        /// <returns>Persian Letter</returns>
        public static string GetLetter(long number)
        {
            return convertNumberToString(number);
        }

        static string[] s_0_9 = new string[] {"صفر", "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه" };
        static string[] s_10_19 = new string[] { "ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده" };
        static string[] s_20_90 = new string[] { "بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود" };
        static string[] s_100_900 = new string[] { "صد", "دویست", "سیصد", "چهارصد", "پانصد", "ششصد", "هفتصد", "هشتصد", "نهصد" };
        static string[] s_Parts = new string[] { "هزار", "میلیون", "میلیارد", "تريليون" };
        static string splitter = " و ";
        static string veryBig = "تعریف نشده";
        static string negative = "منفی";

        protected static string getPart(long numberIn3)
        {
            if (numberIn3.ToString().Length > 3)
            {
                return "";
            }

            string number = numberIn3.ToString();

            switch (number.Length)
            {
                case 1:
                    number = "00" + number;
                    break;
                case 2:
                    number = "0" + number;
                    break;
            }

            string outString = "";

            int n1 = int.Parse(number.Substring(0, 1));
            int n2 = int.Parse(number.Substring(1, 1));
            int n3 = int.Parse(number.Substring(2, 1));

            if (n1 != 0)
            {
                switch (n2)
                {
                    case 0:
                        if (n3 != 0)
                        {
                            outString = s_100_900[n1 - 1] + splitter + s_0_9[n3];
                        }
                        else
                        {
                            outString = s_100_900[n1 - 1];
                        };
                        break;
                    case 1:
                        outString = s_100_900[n1 - 1] + splitter + s_10_19[n3];
                        break;
                    default:
                        if (n3 != 0)
                        {
                            outString = s_100_900[n1 - 1] + splitter + s_20_90[n2 - 2] + splitter + s_0_9[n3];
                        }
                        else
                        {
                            outString = s_100_900[n1 - 1] + splitter + s_20_90[n2 - 2];
                        }
                        break;
                }
            }
            else
            {
                switch (n2)
                {
                    case 0:
                        if (n3 != 0)
                        {
                            outString = s_0_9[n3];
                        }
                        else
                        {
                            outString = "";
                        }
                        break;
                    case 1:
                        outString = s_10_19[n3];
                        break;
                    default:
                        if (n3 != 0)
                        {
                            outString = s_20_90[n2 - 2] + splitter + s_0_9[n3];
                        }
                        else
                        {
                            outString = s_20_90[n2 - 2];
                        }
                        break;
                }
            };

            return outString;
        }

        protected static string convertNumberToString(long inputNumber)
        {
            long tempNumber = Math.Abs(inputNumber);

            if (tempNumber.ToString().Length == 0)
                return "";

            if (tempNumber == 0)
                return s_0_9[0];

            int partCount = (int)Math.Ceiling((double)tempNumber.ToString().Length / (double)3);

            if (s_Parts.Length < partCount)
                return veryBig;

            int lengthToSelectFirtPart = 0;

            string[] partFullString = new string[partCount];

            for (int i = 0; i < partCount; i++)
            {
                int numberLength3;

                if (i == 0)
                {
                    lengthToSelectFirtPart = tempNumber.ToString().Length - ((partCount - 1) * 3);
                    numberLength3 = int.Parse(tempNumber.ToString().Substring((i * 3), lengthToSelectFirtPart));
                }
                else
                {
                    numberLength3 = int.Parse(tempNumber.ToString().Substring(lengthToSelectFirtPart + ((i - 1) * 3), 3));
                }

                string partInWord = getPart(numberLength3);

                int partIndex = (partCount - 2 - i);
                string partPreFix;

                if (i == partCount - 1)
                {
                    partPreFix = "";
                }
                else
                {
                    partPreFix = s_Parts[partIndex];
                }

                if (i == 0)
                {
                    if (partInWord != "")
                    {
                        partFullString[i] = partInWord + " " + partPreFix;
                    }
                    else
                    {
                        partFullString[i] = "";
                    }
                }
                else
                {
                    if (partFullString[i - 1] != "")
                    {
                        if (partInWord != "")
                        {
                            partFullString[i] = splitter + partInWord + " " + partPreFix;
                        }
                        else
                        {
                            partFullString[i] = "";
                        }
                    }
                    else
                    {
                        if (partInWord != "")
                        {
                            partFullString[i] = splitter + partInWord + " " + partPreFix;
                        }
                        else
                        {
                            partFullString[i] = "";
                        }
                    }
                }
            }
            string outString = "";

            for (int i = 0; i < partFullString.Length; i++)
            {
                outString += partFullString[i];
            }

            if (inputNumber.ToString().Substring(0, 1) == "-")
                outString = outString.Insert(0, negative + " ");

            return outString;
        }

    }
}
